{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "97a67919-2681-4e7e-80c1-2118dd166fa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.neighbors import KNeighborsClassifier as KNN\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.metrics import accuracy_score\n",
    "\n",
    "\n",
    "df = pd.read_excel('手写字体识别.xlsx')\n",
    "\n",
    "X = df.drop(columns='对应数字')\n",
    "y = df['对应数字']\n",
    "#X = StandardScaler().fit_transform(X)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=123)\n",
    "\n",
    "knn = KNN(n_neighbors=5) \n",
    "knn.fit(X_train, y_train)\n",
    "y_pred = knn.predict(X_test)\n",
    "score = accuracy_score(y_pred, y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "dca9ea9b-2334-4060-a433-9e751cdb4c83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.979328165374677"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c9d8b52b-8320-46a5-abd6-418d1f905871",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "各算法准确率：\n",
      "支持向量机: 0.9871\n",
      "随机森林: 0.9922\n",
      "逻辑回归: 0.9897\n",
      "决策树: 0.8372\n",
      "高斯朴素贝叶斯: 0.6615\n"
     ]
    }
   ],
   "source": [
    "#其他算法\n",
    "from sklearn.svm import SVC\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.naive_bayes import GaussianNB\n",
    "\n",
    "\n",
    "scaler = StandardScaler()\n",
    "X_scaled = scaler.fit_transform(X)\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    X_scaled, y, test_size=0.2, random_state=123\n",
    ")\n",
    "\n",
    "models = {\n",
    "    '支持向量机': SVC(random_state=123),\n",
    "    '随机森林': RandomForestClassifier(n_estimators=100, random_state=123),\n",
    "    '逻辑回归': LogisticRegression(max_iter=1000, random_state=123),\n",
    "    '决策树': DecisionTreeClassifier(random_state=123),\n",
    "    '高斯朴素贝叶斯': GaussianNB()\n",
    "}\n",
    "\n",
    "print(\"各算法准确率：\")\n",
    "for name, model in models.items():\n",
    "    model.fit(X_train, y_train)\n",
    "    y_pred = model.predict(X_test)\n",
    "    print(f\"{name}: {accuracy_score(y_test, y_pred):.4f}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5946e0a0-6341-4d06-9ee1-41de9c8ec15b",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
